home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Utilities / OrdColl.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  9.7 KB  |  329 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OrdColl.cpp
  3.  
  4.     Contains:    Implementation of OrderedCollection and OrderedCollectionIterator
  5.  
  6.     Owned by:    Reggie Adkins
  7.  
  8.     Copyright:    © 1993-1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     5/24/96    jpa        Fixed header comment.
  13.          <2>     5/24/96    jpa        1339104: Inlined methods for efficiency
  14.                                     (EditorSetup)
  15.  
  16.     To Do:
  17. */
  18.  
  19. #ifndef _ORDCOLL_
  20. #include "OrdColl.h"
  21. #endif
  22.  
  23. #ifndef _ODNEW_
  24. #include "ODNew.h"
  25. #endif
  26.  
  27. #pragma segment ODCollections
  28.  
  29. //======================================================================================
  30. // Class OrderedCollection
  31. //======================================================================================
  32.  
  33. //------------------------------------------------------------------------------
  34. // OrderedCollection::OrderedCollection
  35. //------------------------------------------------------------------------------
  36.  
  37. OrderedCollection::OrderedCollection()
  38. {
  39.     fHeap = kODNULL;
  40. }
  41.  
  42. //------------------------------------------------------------------------------
  43. // OrderedCollection::OrderedCollection
  44. //------------------------------------------------------------------------------
  45.  
  46. OrderedCollection::OrderedCollection(ODMemoryHeapID where)
  47. {
  48.     fHeap = where;
  49. }
  50.  
  51. // OrderedCollection::~OrderedCollection
  52. //------------------------------------------------------------------------------
  53.  
  54. OrderedCollection::~OrderedCollection()
  55. {
  56.     this->RemoveAll();
  57. }
  58.  
  59. //------------------------------------------------------------------------------
  60. // OrderedCollection::AddFirst
  61. //------------------------------------------------------------------------------
  62.  
  63. void OrderedCollection::AddFirst(ElementType element)
  64. {
  65.     ValueLink* newLink = this->CreateNewLink(element);
  66.     fImplementation.AddFirst(newLink);
  67. }
  68.  
  69. //------------------------------------------------------------------------------
  70. // OrderedCollection::AddLast
  71. //------------------------------------------------------------------------------
  72.  
  73. void OrderedCollection::AddLast(ElementType element)
  74. {
  75.     ValueLink* newLink = CreateNewLink(element);
  76.     fImplementation.AddLast(newLink);
  77. }
  78.  
  79.  
  80. //------------------------------------------------------------------------------
  81. // OrderedCollection::AddBefore
  82. //------------------------------------------------------------------------------
  83.  
  84. void OrderedCollection::AddBefore(ElementType existing, ElementType tobeadded)
  85. {
  86.     LinkedList* impl = (LinkedList*) &fImplementation; // cast away const
  87.     LinkedListIterator iter(impl);
  88.     ValueLink* aLink = (ValueLink*) iter.First();
  89.     while (aLink != kODNULL)
  90.     {
  91.         ElementType v = ((ValueLink*) aLink)->GetValue();
  92.  
  93.         if (this->ElementsMatch(v,existing))    
  94.         {
  95.             ValueLink* newLink = CreateNewLink(tobeadded);
  96.             fImplementation.AddBefore(*aLink, newLink);
  97.             aLink = kODNULL;
  98.         }
  99.         else
  100.             aLink = (ValueLink*) iter.Next();
  101.     }
  102. }
  103.  
  104. //------------------------------------------------------------------------------
  105. // OrderedCollection::AddAfter
  106. //------------------------------------------------------------------------------
  107.  
  108. void OrderedCollection::AddAfter(ElementType existing, ElementType tobeadded)
  109. {
  110.     LinkedList* impl = (LinkedList*) &fImplementation; // cast away const
  111.     LinkedListIterator iter(impl);
  112.     ValueLink* aLink = (ValueLink*) iter.First();
  113.     while (aLink != kODNULL)
  114.     {
  115.         ElementType v = ((ValueLink*) aLink)->GetValue();
  116.  
  117.         if (this->ElementsMatch(v,existing))    
  118.         {
  119.             ValueLink* newLink = CreateNewLink(tobeadded);
  120.             fImplementation.AddAfter(*aLink, newLink);
  121.             aLink = kODNULL;
  122.         }
  123.         else
  124.             aLink = (ValueLink*) iter.Next();
  125.     }
  126. }
  127.  
  128. //------------------------------------------------------------------------------
  129. // OrderedCollection::After
  130. //------------------------------------------------------------------------------
  131.  
  132. ElementType OrderedCollection::After(ElementType existing) const
  133. {
  134.     ValueLink* linkAfter = kODNULL;
  135.  
  136.     LinkedList* impl = (LinkedList*) &fImplementation; // cast away const
  137.     LinkedListIterator iter(impl);
  138.  
  139.     for (ValueLink* link = (ValueLink*) iter.First(); iter.IsNotComplete(); link = (ValueLink*) iter.Next())
  140.     {
  141.         ElementType v = ((ValueLink*) link)->GetValue();
  142.  
  143.         if (this->ElementsMatch(v,existing))    
  144.         {
  145.             linkAfter = (ValueLink*) fImplementation.After(*link);
  146.             break;
  147.         }
  148.     }
  149.     
  150.     return linkAfter ? linkAfter->GetValue() : (ElementType)kODNULL;
  151. }
  152.  
  153. //------------------------------------------------------------------------------
  154. // OrderedCollection::Before
  155. //------------------------------------------------------------------------------
  156.  
  157. ElementType OrderedCollection::Before(ElementType existing) const
  158. {
  159.     ValueLink* linkBefore = kODNULL;
  160.  
  161.     LinkedList* impl = (LinkedList*) &fImplementation; // cast away const
  162.     LinkedListIterator iter(impl);
  163.  
  164.     for (ValueLink* link = (ValueLink*) iter.First(); iter.IsNotComplete(); link = (ValueLink*) iter.Next())
  165.     {
  166.         ElementType v = ((ValueLink*) link)->GetValue();
  167.  
  168.         if (this->ElementsMatch(v,existing))    
  169.         {
  170.             linkBefore = (ValueLink*) fImplementation.Before(*link);
  171.             break;
  172.         }
  173.     }
  174.     
  175.     return linkBefore ? linkBefore->GetValue() : (ElementType)kODNULL;
  176. }
  177.  
  178. //------------------------------------------------------------------------------
  179. // OrderedCollection::First
  180. //------------------------------------------------------------------------------
  181.  
  182. ElementType OrderedCollection::First() const
  183. {
  184.     ValueLink* firstLink = (ValueLink*) fImplementation.First();
  185.     return firstLink ? firstLink->GetValue() : (ElementType)kODNULL;
  186. }
  187.  
  188. //------------------------------------------------------------------------------
  189. // OrderedCollection::Last
  190. //------------------------------------------------------------------------------
  191.  
  192. ElementType OrderedCollection::Last() const
  193. {
  194.     ValueLink* lastLink = (ValueLink*) fImplementation.Last();
  195.     return lastLink ? lastLink->GetValue() : (ElementType)kODNULL;
  196. }
  197.  
  198. //------------------------------------------------------------------------------
  199. // OrderedCollection::RemoveFirst
  200. //------------------------------------------------------------------------------
  201.  
  202. ElementType    OrderedCollection::RemoveFirst()
  203. {
  204.     ValueLink* aLink = (ValueLink*) fImplementation.RemoveFirst();
  205.     ElementType value = aLink ? aLink->GetValue() : kODNULL;
  206.     delete aLink;
  207.     return value;
  208. }
  209.  
  210. //------------------------------------------------------------------------------
  211. // OrderedCollection::RemoveLast
  212. //------------------------------------------------------------------------------
  213.  
  214. ElementType    OrderedCollection::RemoveLast()
  215. {
  216.     ValueLink* aLink = (ValueLink*) fImplementation.RemoveLast();
  217.     ElementType value = aLink ? aLink->GetValue() : kODNULL;
  218.     delete aLink;
  219.     return value;
  220. }
  221.  
  222. //------------------------------------------------------------------------------
  223. // OrderedCollection::Remove
  224. //------------------------------------------------------------------------------
  225.  
  226. ODBoolean OrderedCollection::Remove(ElementType existing)
  227. {
  228.     LinkedList* impl = (LinkedList*) &fImplementation; // cast away const
  229.     LinkedListIterator iter(impl);
  230.     ValueLink* aLink = (ValueLink*) iter.First();
  231.     ODBoolean removed = kODFalse;
  232.     while (aLink != kODNULL)
  233.     {
  234.         ElementType v = ((ValueLink*) aLink)->GetValue();
  235.  
  236.         if (this->ElementsMatch(v,existing))
  237.         {
  238.             fImplementation.Remove(*aLink);
  239.             delete aLink;
  240.             removed = kODTrue;
  241.             aLink = kODNULL;    
  242.         }
  243.         else
  244.             aLink = (ValueLink*) iter.Next();
  245.     }
  246.     return removed;
  247. }
  248.  
  249. //------------------------------------------------------------------------------
  250. // OrderedCollection::DeleteAll
  251. //------------------------------------------------------------------------------
  252.  
  253. void OrderedCollection::DeleteAll()
  254. {
  255.     Link* link = fImplementation.RemoveFirst();
  256.     while (link != kODNULL)
  257.     {
  258.         ElementType value = ((ValueLink*) link)->GetValue();
  259.         delete value;
  260.         delete link;
  261.         link = fImplementation.RemoveFirst();
  262.     }
  263. }
  264.  
  265. //------------------------------------------------------------------------------
  266. // OrderedCollection::Contains
  267. //------------------------------------------------------------------------------
  268.  
  269. ODBoolean    OrderedCollection::Contains(ElementType existing) const
  270. {
  271.     LinkedList* impl = (LinkedList*) &fImplementation; // cast away const
  272.     LinkedListIterator iter(impl);
  273.     ValueLink* aLink = (ValueLink*) iter.First();
  274.     while (aLink != kODNULL)
  275.     {
  276.         ElementType v = ((ValueLink*) aLink)->GetValue();
  277.  
  278.         if (this->ElementsMatch(v,existing))
  279.         {
  280.             return kODTrue;    
  281.         }
  282.         else
  283.             aLink = (ValueLink*) iter.Next();
  284.     }    
  285.     return kODFalse;
  286. }
  287.  
  288. //------------------------------------------------------------------------------
  289. // OrderedCollection::CreateIterator
  290. //------------------------------------------------------------------------------
  291.  
  292. OrderedCollectionIterator* OrderedCollection::CreateIterator()
  293. {
  294.     return new(fHeap) OrderedCollectionIterator(this);
  295. }
  296.  
  297.  
  298. //------------------------------------------------------------------------------
  299. // OrderedCollection::CreateNewLink
  300. //------------------------------------------------------------------------------
  301.  
  302. ValueLink*    OrderedCollection::CreateNewLink(ElementType value) const
  303. {
  304.     return new (fHeap) ValueLink(value);
  305. }
  306.  
  307. //------------------------------------------------------------------------------
  308. // OrderedCollection::ElementsMatch
  309. //------------------------------------------------------------------------------
  310.  
  311. ODBoolean    OrderedCollection::ElementsMatch(ElementType v1,ElementType v2) const
  312. {
  313.     return (v1 == v2);
  314. }
  315.  
  316. //======================================================================================
  317. // OrderedCollectionIterator
  318. //======================================================================================
  319.  
  320. //------------------------------------------------------------------------------
  321. // OrderedCollectionIterator::OrderedCollectionIterator
  322. //------------------------------------------------------------------------------
  323.  
  324. OrderedCollectionIterator::OrderedCollectionIterator(OrderedCollection* collection)    
  325.     : fImplementation(collection ? &(collection->fImplementation) : (LinkedList*)kODNULL)
  326. {
  327.     fCollection =  collection;
  328. }
  329.